unit Unit16;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, db, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  DBGrids, IBQuery, IBDatabase, ZDataset;

type

  { TForm16 }

  TForm16 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    ibq1: TIBQuery;
    ibq2: TIBQuery;
    ibt1: TIBTransaction;
    ibt2: TIBTransaction;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form16: TForm16;

implementation
uses
  unit17, unit5;
{$R *.lfm}

{ TForm16 }

procedure TForm16.Button4Click(Sender: TObject);
begin
  close;
end;

procedure TForm16.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if ibt1.Active then ibq1.Transaction.Commit;
end;

// dodaj
procedure TForm16.Button1Click(Sender: TObject);
begin
  form17.Caption:='Nowa grupa';
  form17.Edit1.Clear;
  form17.ActiveControl:=form17.Edit1;
  if form17.ShowModal = mrOK then
  begin
    ibq2.SQL.Clear;
    ibq2.SQL.Add('insert into prac_grup (nazwadl) values (:a)');
    ibq2.ParamByName('a').AsString:=form17.Edit1.Text;
    try
      ibq2.Transaction.StartTransaction;
      ibq2.ExecSQL;
    except
      ibq2.Transaction.Rollback;
    end;
    FormShow(nil);
  end;
end;

// edytuj
procedure TForm16.Button2Click(Sender: TObject);
var
  key : integer;
begin
  key:=ibq1.FieldByName('prac_grup_idx').AsInteger;
  form17.Caption:='Edycja grupy';
  form17.Edit1.Text:=ibq1.FieldByName('nazwadl').AsString;
  form17.ActiveControl:=form17.Edit1;
  if form17.ShowModal = mrOK then
  begin
    ibq2.SQL.Clear;
    ibq2.SQL.Add('update prac_grup set nazwadl = :a where prac_grup_idx = :b');
    ibq2.ParamByName('a').AsString:=form17.Edit1.Text;
    ibq2.ParamByName('b').AsInteger:=key;
    try
      ibq2.Transaction.StartTransaction;
      ibq2.ExecSQL;
      ibq2.Transaction.Commit;
    except
      ibq2.Transaction.Rollback;
    end;
    FormShow(nil);
  end;
end;

// kasuj
procedure TForm16.Button3Click(Sender: TObject);
var
  key : integer;
begin
  key:=ibq1.FieldByName('prac_grup_idx').AsInteger;
  if form5.ShowModal = mrOK then
  begin
    if ibq2.Active then ibq2.Close;
    ibq2.SQL.Clear;
    ibq2.SQL.Add('delete from prac_grup where prac_grup_idx = :b');
    ibq2.ParamByName('b').AsInteger:=key;
    try
      ibq2.Transaction.StartTransaction;
      ibq2.ExecSQL;
      ibq2.Transaction.Commit;
    except
      ibq2.Transaction.Rollback;
    end;
    FormShow(nil);
  end;
end;

procedure TForm16.FormShow(Sender: TObject);
var
  i : integer;
begin
  if ibt1.Active then ibt1.Commit;
  ibq1.SQL.Clear;
  ibq1.SQL.Add('select * from prac_grup order by upper(nazwadl)');
  try
    ibq1.Transaction.StartTransaction;
    ibq1.Open;
    for i:=0 to DBGrid1.Columns.Count-1 do
    begin
      if pos('_IDX',UpperCase(DBGrid1.Columns[i].FieldName)) <> 0 then DBGrid1.Columns[i].Visible:=false;
    end;
  except
    ibq1.Transaction.Rollback;
  end;
end;

end.

